var express = require('express');
const UserModel = require('../models/UserModel');
const BusinessModel = require('../models/BusinessGoods');
const JWT = require('../util/JWT');
var router = express.Router();

/* GET users listing. */
router.post('/login', async (req, res) => {
  const { phone, key } = req.body
  const userList = await UserModel.find({ phone, key })

  if (userList.length > 0) {
    const token = JWT.generate({
      _id: userList[0]._id,
      name: userList[0].name,
    }, '1d')
    res.setHeader('Access-Control-Expose-Headers', 'Authorization')
    res.header("Authorization", token)
    res.send({
      ActionType: "OK",
      userList
    })
  } else {
    res.send({
      message: "该用户不存在"
    })
  }
});

module.exports = router;
